package cn.hznc.controller.codescanner;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.controller.service.codescanner.CodeScannerService;
import cn.hznc.controller.service.inventory.InventoryLocationService;
import cn.hznc.domain.entity.AssemblyToolInventoryEntity;
import cn.hznc.domain.entity.CodeScannerConfigureEntity;
import cn.hznc.domain.entity.CodeScannerEntity;
import cn.hznc.domain.entity.ToolPartEntity;
import cn.hznc.domain.request.codescanner.EditConfigurationReq;
import cn.hznc.domain.request.codescanner.WriteParamsReq;
import cn.hznc.domain.request.codescanner.WriteShankParamReq;
import cn.hznc.domain.request.codescanner.WriteUniqueIdReq;
import cn.hznc.domain.request.inventory.InboundAssemblyReq;
import cn.hznc.domain.request.inventory.OutboundUniqueAssemblyReq;
import cn.hznc.domain.request.inventory.UniqueAssemblyToolVo;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.exception.ToolManageException;
import cn.hznc.mapper.AssemblyToolInventoryMapper;
import cn.hznc.mapper.ToolPartMapper;
import cn.hznc.repository.AllotEmployeeRepository;
import cn.hznc.repository.InventoryLocationRespository;
import cn.hznc.repository.WarehouseRespository;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/7/26 11:16
 */
@RestController
@Slf4j
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/codeScanner")
public class CodeScannerController {

    @Autowired
    private CodeScannerService codeScannerService;

    @Resource
    private ToolPartMapper toolPartMapper;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private AssemblyToolInventoryMapper assemblyToolInventoryMapper;

    @Resource
    private AllotEmployeeRepository allotEmployeeRepository;

    @Resource
    private WarehouseRespository warehouseRespository;

    @Resource
    private InventoryLocationService inventoryLocationService;

    @RequestMapping(value = "/qryConfiguration", method = RequestMethod.GET)
    public ResponseBody qryConfiguration(@RequestParam("operateType") String operateType) {
        return ResponseBody.success(codeScannerService.qryConfiguration(operateType));
    }

    @RequestMapping(value = "/editConfiguration", method = RequestMethod.POST)
    @Log(title = "修改扫码枪配置" , businessType = BusinessType.UPDATE)
    public ResponseBody editConfiguration(@RequestBody EditConfigurationReq editConfigurationReq) {
        return ResponseBody.success(codeScannerService.editConfiguration(editConfigurationReq));
    }

    @RequestMapping(value = "/editAddressConfiguration", method = RequestMethod.GET)
    @Log(title = "修改扫码枪COM口" , businessType = BusinessType.UPDATE)
    public ResponseBody editAddressConfiguration(@RequestParam("address") String address) {
        return ResponseBody.success(codeScannerService.editAddressConfiguration(address));
    }

    @RequestMapping(value = "/qryAddressConfiguration", method = RequestMethod.GET)
    public ResponseBody qryAddressConfiguration() {
        return ResponseBody.success(codeScannerService.qryAddressConfiguration());
    }

    @RequestMapping(value = "/deleteConfiguration", method = RequestMethod.GET)
    @Log(title = "删除扫码枪配置" , businessType = BusinessType.DELETE)
    public ResponseBody deleteConfiguration(@RequestParam("id") Integer id) {
        return ResponseBody.success(codeScannerService.deleteConfiguration(id));
    }

    @RequestMapping(value = "/writeShankParam", method = RequestMethod.POST)
    public ResponseBody writeShankParam(@RequestBody WriteShankParamReq writeShankParamReq) {
        try {
            return ResponseBody.success(codeScannerService.writeShankParam(writeShankParamReq));
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(),e.getMessage());
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(),e.getMessage());
        }
    }

//    @RequestMapping(value = "/readUniqueId",method = RequestMethod.POST)
//    public ResponseBody readUniqueId(@RequestBody CodeScannerConfigureEntity codeScannerConfigureEntity){
//        try {
//            return ResponseBody.success(codeScannerService.readUniqueId(codeScannerConfigureEntity));
//        } catch (ToolManageException e) {
//            return ResponseBody.error(e.getCode() , e.getMessage());
//        }
//    }

//    @RequestMapping(value = "/writeUniqueId",method = RequestMethod.POST)
//    public ResponseBody writeUniqueId(@RequestBody WriteUniqueIdReq writeUniqueIdReq){
//        try {
//            return ResponseBody.success(codeScannerService.writeUniqueId(writeUniqueIdReq));
//        } catch (ToolManageException e) {
//            return ResponseBody.error(e.getCode() , e.getMessage());
//        }
//    }

    /**
     * 输入刀具号或者物料编码生成对应刀具rfid并写入
     *
     * @param writeParamsReq
     * @return
     */
    @RequestMapping(value = "/writeParams", method = RequestMethod.POST)
    public ResponseBody writeParams(@RequestBody WriteParamsReq writeParamsReq) {
        try {
            return ResponseBody.success(codeScannerService.writeParamsReload(writeParamsReq));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        } catch (ToolLifeCycleException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 直接传刀具rfid并写入
     *
     * @param writeParamsReq
     * @return
     */
    @RequestMapping(value = "/writeParamsReload", method = RequestMethod.POST)
    public ResponseBody writeParamsReload(@RequestBody WriteParamsReq writeParamsReq) {
        try {
            return ResponseBody.success(codeScannerService.writeParams(writeParamsReq));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/readParams", method = RequestMethod.GET)
    public ResponseBody readParams(@RequestParam("operateType") String operateType , @RequestParam("url") String url , @RequestParam("com") String com) {
        try {
            return ResponseBody.success(codeScannerService.readParams(operateType , url , com));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        }
    }

    @RequestMapping(value = "/addOrEditUrl", method = RequestMethod.POST)
    public ResponseBody addOrEditUrl(@RequestBody CodeScannerEntity codeScannerEntity) {
        return ResponseBody.success(codeScannerService.addOrEditUrl(codeScannerEntity));
    }

    @RequestMapping(value = "/selectCodeScannerUrls", method = RequestMethod.GET)
    public ResponseBody selectCodeScannerUrls() {
        return ResponseBody.success(codeScannerService.selectCodeScannerUrls());
    }

    @RequestMapping(value = "/deleteCodeScannerUrl", method = RequestMethod.GET)
    @Log(title = "删除扫码枪地址" , businessType = BusinessType.DELETE)
    public ResponseBody deleteCodeScannerUrl(@RequestParam("id") Integer id) {
        return ResponseBody.success(codeScannerService.deleteCodeScannerUrl(id));
    }

}
